package com.heart.sell.service.impl;

import com.heart.sell.dto.OrderDTO;
import com.heart.sell.enums.ResponseCodeEnum;
import com.heart.sell.exception.SellException;
import com.heart.sell.service.OrderService;
import com.heart.sell.service.PayService;
import com.heart.sell.util.JsonUtil;
import com.heart.sell.util.MathUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author Luo Zhen
 * @create 2019-04-26 14:53
 */
@Service
@Slf4j
public class DefaultPayServiceImpl implements PayService {

    public static final String ORDER_NAME = "微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;
    @Autowired
    private OrderService orderService;

    @Override
    public PayResponse create(OrderDTO orderDTO) {

        PayRequest payRequest = new PayRequest();
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);

        log.info("【微信支付】request={}", JsonUtil.toJson(payRequest));
        PayResponse response = bestPayService.pay(payRequest);
        log.info("【微信支付】response={}", JsonUtil.toJson(response));
        return response;
    }

    @Override
    public PayResponse notify(String notify) {
        // 1.验证签名
        // 2.支付是否存在
        // 3.支付金额是否一致
        // 4.支付人是否等于下单人

        PayResponse payResponse = bestPayService.asyncNotify(notify);
        log.info("【微信支付】异步通知,payResponse={}", JsonUtil.toJson(payResponse));

        // 修改订单状态
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());
        if (orderDTO == null) {
            log.error("【微信支付】异步通知,订单不存在,orderId={}", payResponse.getOrderId());
            throw new SellException(ResponseCodeEnum.PRODUCT_NOT_EXIST);
        }
        // 判断金额是否一致
        if (!MathUtil.equals(payResponse.getOrderAmount(), orderDTO.getOrderAmount().doubleValue())) {
            log.error("【微信支付】异步通知,订单不一致,orderId={},微信通知金额={},系统金额={}",
                    payResponse.getOrderId(),
                    payResponse.getOrderAmount(),
                    orderDTO.getOrderAmount());
            throw new SellException(ResponseCodeEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        // 修改订单状态
        orderService.paid(orderDTO);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信退款】request={}", JsonUtil.toJson(refundRequest));

        RefundResponse response = bestPayService.refund(refundRequest);
        log.info("【微信退款】response={}", JsonUtil.toJson(response));
        return response;
    }
}
